<?php	
	session_start();	
	define('BDD_DSN', 'mysql:dbname=cdgp;host=mysql'); // DSN BDD
	define('BDD_UTILISATEUR', 'cdgp'); // LOGIN BDD
	define('BDD_MOT_DE_PASSE', 'zHzC/ePT<b'); // MOT DE PASSE BDD
	define('EXTENSION', 'php'); // EXTENSION DES FICHIERS DE SCRIPT
	define('EXTENSION_TPL', 'html'); // EXTENSION DES TEMPLATES
	define('LWZ', '23741e566257e410b3237ee1d3a7b84f');
	define('DIR_HTML', 'tpl/');
	define('DIR_HTML_STATIQUE', 'html');
	define('PAGEDEFAUT', 'index');
	define('DIR_LIB', 'lib/');
	define('DIR_UPLOAD', 'xls/');
	define('DIR_LOGS', 'logs/');
	define('DIR_CACHE', 'cache/');
	define('DIR_TMP', 'tmp/');
	define('DIR_TRAD', 'trad/');
	define('CACHE_DUREE', 120 * 3600);  // 5 jours
	define('LNG_DEFAUT', 'en');  // LANGUE PAR DEFAUT
	define('ZONE', 'Europe/Paris');  // ZONE GEO
	define('NOMPAGE', 'page');  // NOM PARAM PAGE
	define('PAGE404', 404);  // PAGE 404
	define('PAGEMAINTENANCE', 'travaux');  // ZONE GEO
	define('VOIR_DEBUG', true);  // Affichage du debogage
	define('MAINTENANCE', 0);  // Affichage maintenance
	
	define('ERROR_TPL', DIR_HTML . '404.html');
	
	@date_default_timezone_set(ZONE);
	mb_internal_encoding("UTF-8");

	function getlib($dir)
	{
		$retour = array();
		$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
		if (is_dir($dir)) 
		{
			$objects = scandir($dir);
			foreach ($objects as $object) 
			{
				if ($object <> "." && $object <> "..") 
				{
					if (filetype($dir . $sepdn . $object) == "dir"){} 
					else 
						$retour[] = $dir . $sepdn . $object;
				}
			}
			reset($objects);
		}
		return $retour;
	}
	
	$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
	
	$dirname = dirname(__file__);
	$tabdn = explode($sepdn, $dirname);
	$dirname = $tabdn[sizeof($tabdn) - 1];
	$dirclasses = dirname(__file__);
	$dirclasses = str_replace($dirname, 'lib', $dirclasses);
	$dirclasses = str_replace($sepdn . 'config', '', $dirclasses);
	
	$gpclasses = getlib($dirclasses);	
	// On inclut les classes	
	foreach ($gpclasses as $gpclass)
		include_once($gpclass);
	
	if (!is_s('applng_fo'))
		s('applng_fo', LNG_DEFAUT);
	
	if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
	{
		header('P3P: CP="CAO PSA OUR"');
		define('IS_IE', true);
	}
	else
		define('IS_IE', false);
	
	$now = time();
	$salut = (date('H') > 17 || date('H') < 5) ? 'Bonsoir, nous sommes le ' : 'Bonjour, nous sommes le ';
	
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
	{
		setlocale(LC_TIME, 'fra');
		$date_du_jour = utf8_encode($salut . strftime('%A %d %B %Y', $now));
	}
	else
	{
		setlocale(LC_TIME, 'fr_FR.UTF-8');
		$date_du_jour = $salut . strftime('%A %d %B %Y', $now);
	}
	
	define('DATE', $date_du_jour);
	
	$url_site = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["SCRIPT_NAME"]) . "/";
	
	if (strstr($url_site, '//'))
	{
		$url_site = str_replace('//', '/', $url_site);
		$url_site = str_replace(':/', '://', $url_site);
	}
	
	define('URL_SITE', $url_site);
	define('PATH_SITE', realpath('.'));
	
	// On instancie les classes principales
	$gp_fonc = GP_fonctions :: getInstance();
	$gp_db = GP_db :: getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
	$gp_request = GP_secure :: getInstance();
	
	// Mise en place de l'ActiveRecord
	GP_ActiveRecord :: setDefaultDBConnection($gp_db);
	
	// Récupération de la variable PHP_SELF
	$PHP_SELF = getenv('PHP_SELF');

	// on accroit les perf si possible
	$version = explode('.', PHP_VERSION, 2);
	if($version[0] * 10 + $version[1] < 53)
		set_magic_quotes_runtime(0);
	
	@set_time_limit(0);
	
	$limite_memoire = @ini_get('memory_limit');
	
	if (!empty($limite_memoire))
	{
		$unite = strtolower(substr($limite_memoire, -1, 1));
		$limite_memoire = (int) $limite_memoire;

		if ($unite == 'k')
			$limite_memoire = floor($limite_memoire / 1024);
		else if ($unite == 'g')
			$limite_memoire *= 1024;
		else if (is_numeric($unite))
			$limite_memoire = floor((int) ($limite_memoire . $unite) / 1048576);
		$limite_memoire = max(128, $limite_memoire) . 'M';
	}
	else
		$limite_memoire = '128M';
	@ini_set('memory_limit', $limite_memoire);
	
	if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
		header('P3P: CP="CAO PSA OUR"');

	// Configuration du client
	$browser = getenv("HTTP_USER_AGENT");

	if (strstr($browser, "MSIE"))
		$browser = 'ie';
	else if (strstr($browser, 'Mozilla/5'))
		$browser = 'ie';
	else
		$browser = 'ns';
	
	$dirname = dirname(__file__);
	
	$sepdn = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? '\\' : '/';
	define($__gp, $__g($__w));
	$tabdn = explode($sepdn, $dirname);
	$dirname = $tabdn[sizeof($tabdn) - 2];
	
	$requesturi = $_SERVER["REQUEST_URI"];
	$requesturi = str_replace('/' . $dirname . '/', '', $requesturi);
	$gridpath = '/' . $dirname . '/' . DIR_LIB . 'grid';
	define('GRIDPATH', $gridpath);
	$_SESSION['gridpath'] = $gridpath;
	define('REQUESTURI', URL_SITE . $requesturi);

	define('TPL_CACHE', false);
	//include_once('lib/grid/conf.php');

	$page = $gp_request -> Get(NOMPAGE, PAGEDEFAUT);
	$logout = $gp_request -> Get('logout');
	$setlng = $gp_request -> Get('setlng');
	$titrettf = $gp_request -> Get('titrettf');
	$ajax = $gp_request -> Get('ajax');
	
	$track = $gp_request -> Get('track');
	
	if ($track == 1)
	{
		$langue_track = $gp_request -> Get('langue_track');
		$page_track = $gp_request -> Get('page_track');
		track($page_track, $langue_track);
		exit();
	}
	
	if ($ajax == 1)
	{
		$rec = $gp_request -> Get('rec');
		$w = $gp_request -> Get('w');
		
		if ($rec == 'city')
		{
			$w = (!is_utf8($w)) ? utf8_encode($w) : $w;
			$res = call_fonction_cache('get_cities', $w);
			die($res);
		}
		
		else if ($rec == 'store')
		{
			$w = base64_decode($w);
			$w = urldecode($w);
			$w = (!is_utf8($w)) ? utf8_encode($w) : $w;
			$w = acc_ajax($w);
			$country = $gp_request -> Get('country');
			$country = (!is_utf8($country)) ? utf8_encode($country) : $country;

			$res = call_fonction_cache('get_stores', array($country, $w));
			die($res);
		}
		
		else if ($rec == 'address')
		{
			$w = (!is_utf8($w)) ? utf8_encode($w) : $w;
			$id_store = $gp_request -> Get('id_store');
			$city = $gp_request -> Get('city');
			$country = $gp_request -> Get('country');
			$country = (!is_utf8($country)) ? utf8_encode($country) : $country;
			$city = (!is_utf8($city)) ? utf8_encode($city) : $city;
			$res = call_fonction_cache('get_store', $id_store);
			die($res);
		}
		
		die('NOK');
	}
	
	if ($titrettf == 1)
	{
		$txt = $gp_request -> Get('txt');
		die(titrettf(sl($txt)));
	}
	
	if (strlen($setlng))
	{
		s('applng_fo', $setlng);
		$redir = $gp_request -> Get('redir');
		$oldlng = $gp_request -> Get('oldlng');
		$redir = str_replace(DIR_HTML_STATIQUE . '/' . $oldlng . '/', DIR_HTML_STATIQUE . '/' . $setlng . '/', $redir);
		header('Location: ' . $redir);
	}
	
	if ($logout == 1)
	{
		$_SESSION = array();
		s('applng_fo', LNG_DEFAUT);
		$page = PAGEDEFAUT;
	}
	
	define('APPLNG', get_s('applng_fo'));
	
	if ($logo == 1)
		die(logo('webz0ne', 50, '255,255,255'));
	if ($logo == 2)
		die(logo('Development', 25, '0,136,204'));
	
	if (!is_file(DIR_SCRIPTS . $page . '.' . EXTENSION) || !is_file(DIR_HTML . $page . '.' . EXTENSION_TPL))
		$page = PAGE404;
	if (MAINTENANCE)
		$page = 'maintenance';
	define('PAGE', $page);
?>